package com.erp.serviceImpl.purchaseServiceImpl;

import com.erp.dao.DepotHeadMapper;
import com.erp.pojo.DepotHead;
import com.erp.pojo.DepotHeadExample;
import com.erp.service.purchaseService.DepotHeadInService;
import com.erp.utils.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;



@Service
public class DepotHeadInServiceImpl implements DepotHeadInService {

    @Autowired
    private DepotHeadMapper depotHeadMapper;
    @Override
    public Msg getAllDep(
            int page,int limit,
            String number, String operpersonname,
            Date billTimeBefore, Date billTimeBack) {
        DepotHeadExample example = new DepotHeadExample();
        DepotHeadExample.Criteria criteria = example.createCriteria();
        if(number!=null){
            number = "%"+number+"%";
            criteria.andNumberLike(number);
        }
        if(operpersonname!=null){
            operpersonname = "%"+operpersonname+"%";
            criteria.andOperpersonnameLike(operpersonname);
        }
        if(billTimeBefore!=null && billTimeBack!=null){
            criteria.andCreatetimeBetween(billTimeBefore,billTimeBack);
        }else if(billTimeBefore!=null){
            criteria.andCreatetimeGreaterThanOrEqualTo(billTimeBefore);
        } else if(billTimeBack!=null){
            criteria.andCreatetimeLessThanOrEqualTo(billTimeBack);
        }
        String subtype="采购";
        criteria.andSubtypeEqualTo(subtype);
//        准备分页
        PageHelper.startPage(page,limit);
//        检查是否为条件查询
        List<DepotHead> depotHeads=depotHeadMapper.selectByExample(example);
//        进行分页
        PageInfo pageInfo = new PageInfo(depotHeads,10);
        Msg msg = new Msg();
        msg.setCode(0);
        msg.setMsg("");
        msg.setCount(pageInfo.getTotal());
        msg.setData(pageInfo.getList());
        return msg;
    }

    /*@Override
    public void addDepotHead(Long id, String type, String subtype, String defaultnumber, String number, String operpersonname, Date createtime, Long organid, String remark, Boolean status) {
        DepotHead depotHead = new DepotHead();
        depotHead.setId(id);
        depotHead.setType(type);
        depotHead.setSubtype(subtype);
        depotHead.setDefaultnumber(defaultnumber);
        depotHead.setNumber(number);
        depotHead.setOperpersonname(operpersonname);
        depotHead.setCreatetime(createtime);
        depotHead.setOrganid(organid);
        depotHead.setRemark(remark);
        depotHead.setStatus(status);
        depotHeadMapper.insert(depotHead);
        System.out.println("插入成功");
    }*/
    @Override
    public void addDepotHead(DepotHead depotHead) {
        depotHeadMapper.insert(depotHead);
    }

    @Override
    public void deleteDepoetHead(Long id) {
        depotHeadMapper.deleteByPrimaryKey(id);
    }
}
